import {createGroupBuyXCX,getGroupGoodsGroupOrderInfoXCX,cancelGroupOrder2,cancelGroupOrder1,cancelGroupOrder,commitReceiveGoods1,commitReceiveGoods2,commitReceiveGoods} from '../../../Tools/HTTPOpertion.js';
var _lastTime = null
function throttle(fn, gapTime) {
    if (gapTime == null || gapTime == undefined) {
        gapTime = 1500
    }
    return function () {
        let _nowTime = + new Date()
        console.log(_nowTime, _lastTime, gapTime);
        if (_nowTime - _lastTime > gapTime || !_lastTime) {
            fn.apply(this, arguments)   //将this和参数传给原函数
            _lastTime = _nowTime
        }
    }
}
Page({
    data: {
      id: '',
      xqObj: {},
      goods_type:''
    },
    onLoad: function (options) {
      this.setData({
        id:options.id,
        goods_type: options.goods_type
      })
      if (options.goods_type==1){
        wx.setNavigationBarTitle({
          title: '拼团订单'
        })
      } else if (options.goods_type == 2){
        wx.setNavigationBarTitle({
          title: '秒杀订单'
        })
      } else if (options.goods_type == 3) {
        wx.setNavigationBarTitle({
          title: '砍价订单'
        })
      }
        this.id = options.id;
        this.loadData(options.id);
    },
    navToOrderDetail: function() {

    },
    loadData: function (id) {
        var that = this;
        getGroupGoodsGroupOrderInfoXCX({ cnd: id }).then(function (res) {
            console.log(res)
            wx.hideLoading();
            function fn(a) {
                if (typeof a == 'string') { return a };
                var str = a / 100 + '';
                var i = str.indexOf('.');
                if (i != -1) {
                    if (str.length != i + 3) {
                        str += '0';
                    }
                } else {
                    str += '.00';
                }
                return str;
            };
            res.data.group_price = fn(res.data.group_price);
            res.data.price = fn(res.data.price);
            that.setData({
                xqObj: res.data
            })
        }).catch(function (err) {
            wx.hideLoading();
            console.log(err)
        });
    },

    //售后
    shohTap: function () {
        wx.navigateTo({
            url: '../customerService/customerService?store_phone=' + this.data.xqObj.store_phone
        })
    },
    //再次购买
    zcgTap: function () {
        wx.navigateTo({
            url: '../goodDetail/goodDetail?id=' + this.data.xqObj.goods_group_id
        })
    },
    //立即支付
    lijizfTap: function() {
        var that = this;
        var obj = {
            cnd: that.data.xqObj.goods_group_id,
            num: that.data.xqObj.num,
            money: that.data.xqObj.money,
            merchant_id: that.data.xqObj.merchant_id,
            ratio: that.data.xqObj.ratio,
            address_id: that.data.xqObj.person_address_id,
            name: that.data.xqObj.group_name,
            create_person_id: that.data.xqObj.id,
            group_buy_id: that.data.xqObj.group_buy_id,
            orderId: that.data.xqObj.orderId
        };
        console.log(obj);
        createGroupBuyXCX(obj).then(function (res) {
            if (res.data.package) {
                wx.requestPayment({
                    timeStamp: res.data.timeStamp,
                    nonceStr: res.data.nonceStr,
                    package: res.data.package,
                    signType: res.data.signType,
                    paySign: res.data.paySign,
                    success: function (e) {
                        console.log(e);
                        wx.showToast({
                            title: '支付成功',
                            complete: function () {
                                wx.redirectTo({
                                    url: '../DetailsPayment/DetailsPayment?cnd='+that.data.id
                                })
                            }
                        })
                    },
                    fail: function (er) {
                        console.log(er)
                        wx.showToast({
                            title: '支付失败'
                        })
                    },
                })
            }
        }).catch(function (err) {
            wx.hideLoading();
            console.log(err)
        });
    },
    //取消订单
    qxdTap: function (e) {
      console.log(e)
      var p;
      var token = wx.getStorageSync("token");
      var that = this;
      var data = e;
      wx.showModal({
        title: '取消订单',
        content: '取消订单后款项会原路退回',
        success: function (e) {
          if (e.confirm) {
            var obj = {
              orderId: data.currentTarget.dataset.id,
              token: token
            }
            var type1 = data.currentTarget.dataset.goods_type;
            console.log(type1)
            // 拼团
            if (type1 == 1) {
              p = cancelGroupOrder1
              // 砍价
            } else if (type1 == 3) {
              p = cancelGroupOrder
            } else if (type1 == 2) {
              p = cancelGroupOrder2
            }
            p(obj).then(function (res) {
              console.log(res)
              if (res.code == 0) {
                var title = '';
                if (res.data.cancelFlag) {
                  title = '取消成功'
                } else {
                  title = '取消失败'
                };
                wx.showToast({
                  title: title
                });
                setTimeout(function () {
                  that.loadData(that.id);
                }, 1500)
              } else {
                wx.showToast({
                  title: res.message,
                  icon: 'none',
                  duration: 2000
                });
              }

            })
              .catch(function (err) {
                console.log(err)
                wx.showToast({
                  title: err.message,
                  duration: 2000
                })
              })
          }
        }
      })

    },

    //确认收货
    qrsTap: function (e) {
      var that = this;
      var pro;
      var title = '';
      var token = wx.getStorageSync("token");
      var delivery_method = e.currentTarget.dataset.delivery_method;
      if (delivery_method == 1) {
        title = "确认收货"
      } else if (delivery_method == 2) {
        title = "确认自提"
      }
      var obj = {
        orderId: e.currentTarget.dataset.id,
        token: token
      }
      var type1 = e.currentTarget.dataset.goods_type;
      wx.showModal({
        title: title,
        content: title + "后款项无法退回，请谨慎操作",
        success: function (e) {
          if (e.confirm) {

            if (type1 == 1) {
              pro =commitReceiveGoods1
            } else if (type1 == 3) {
              pro =commitReceiveGoods
            } else if (type1 == 2) {
              pro =commitReceiveGoods2
            }
            throttle(function () {
              pro(obj).then(function (res) {
                if (res.code == 0) {
                  that.loadData(that.id);
                } else {
                  wx.showToast({
                    title: res.message,
                    icon: 'none',
                    duration: 2000
                  });
                }

              })
              .catch(function (err) {
                wx.showToast({
                  title: err.message,
                  duration: 2000
                })
              })
            }, 10000)();
          }
        },
        fail: function (err) {
          console.log(err)
        }
      })


    },
    toOrderDetails:function(){
      console.log(this.data.xqObj.goods_group_id)
      console.log(this.data.xqObj)
      console.log(this.data.goods_type)
      if (this.data.goods_type==1){
        wx.navigateTo({
          url: '../makeGroupsDetails/makeGroupsDetails?id=' + this.data.xqObj.goods_group_id,
        })
      } else if (this.data.goods_type == 2){
        wx.navigateTo({
          url: '../miaoshaDetails/miaoshaDetails?id=' + this.data.xqObj.goods_group_id + '&times=123&merchantName=' + this.data.xqObj.merchant_name,
        })
      } else if (this.data.goods_type == 3){
        wx.navigateTo({
          url: '../BargainDetails/BargainDetails?id=' + this.data.id,
        })
      }
    }
})
